<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'home';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$this->model('member')->upmember();
$return['pluginset']['ailogo']=tomedia($return['pluginset']['ailogo']);
$return['basic']=$return['pluginset'];
if($_RQ['op']=='home'){
	if(!empty($_SESSION['openid'])){
		$openid = $_SESSION['openid'];
		$fansfollow=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'openid'=>$_SESSION['openid']),'follow');
	}
	$return['member']= $this->model('member')->getmember();
	$return['openid']= $_SESSION['openid'];
	$return['agent_group']=pdo_getall('jy_aiagent_agent_group',array('isshow'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id','paixu desc');
	foreach($return['agent_group'] as &$nv){
		
	}
	$return['goodslist']=pdo_getall('jy_aiagent_goodslog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$return['llm_models2']=$this->llm_models2;
	$return['yuyuantypes']=$this->yuyuantypes;
	$return['params']=array('type'=>intval($_RQ['type']),'groupid'=>0,'voicesex'=>0,'yuyuantype'=>0);
	$return['post']=array('goodsid'=>intval($_RQ['goodsid']),'msg'=>'','issikao'=>1,'iswww'=>1);
	$return['sharedata']=array(
		'title'=>$return['basic']['share_title'],
		'desc'=>$return['basic']['share_desc'],
		'link'=>custom_url('jy_aiagent/agent//fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$return['basic']['share_image'],
	);
	set_html('title','AI智能体广场');
	exi($return);
}
if($_RQ['op']=='addagent'){
	$postdata=get_postdata();
	$postdata['agentid']=trim($postdata['agentid']);
	if(!$postdata['agentid']){
		exi('请选择智能体！','error');
	}
	$agent=pdo_get('jy_aiagent_agent',array('id'=>$postdata['agentid'],'uniacid'=>$_SESSION['uniacid']));
	$data=array(
			'title'=>trim($agent['title']),
			'logo'=>trim($agent['logo']),
			'desc'=>trim($agent['desc']),
			'voiceid'=>$agent['voiceid'],
			'llm_model'=>trim($agent['llm_model']),
			'ainame'=>trim($agent['ainame']),
			'user_name'=>trim($agent['user_name']),
			'status'=>1,
	);
	$data['voiceid']=$postdata['voiceid']?:$agent['voiceid'];
	$this->model('agent')->adduseragents($data);
	exi('添加成功！','','/index/');
}
if($_RQ['op']=='qiehuan'){
	$postdata=get_postdata();
	if(!$postdata['goodsid']){
		exi('请选择设备！','error');
	}
	if(!$postdata['qie_agentid']){
		exi('请选择智能体！','error');
	}
	$goodslog=pdo_get('jy_aiagent_goodslog',array('id'=>$postdata['goodsid'],'uniacid'=>$_SESSION['uniacid']));
	if($postdata['qie_agenttype']){
		$agent=pdo_get('jy_aiagent_useragent',array('id'=>$postdata['qie_agentid'],'uniacid'=>$_SESSION['uniacid']));
	}else{
		$agent=pdo_get('jy_aiagent_agent',array('id'=>$postdata['qie_agentid'],'uniacid'=>$_SESSION['uniacid']));
	}
	if(!$goodslog['agentid']){
		$agent=$this->model('agent')->addgoodsagent('',$agent);
		pdo_update('jy_aiagent_goodslog',array('agentid'=>$agent['agentid'],'agentapiid'=>$agent['apiid']),array('id'=>$postdata['goodsid'],'uniacid'=>$_SESSION['uniacid']));
	}else{
		$goodsagent=pdo_get('jy_aiagent_useragent',array('id'=>$goodslog['agentid'],'uniacid'=>$_SESSION['uniacid']));
		$goodsagent['title']=$agent['title'];
		$goodsagent['logo']=$agent['logo'];
		$goodsagent['desc']=$agent['desc'];
		$goodsagent['voiceid']=$agent['voiceid'];
		$goodsagent['llm_model']=$agent['llm_model'];
		$goodsagent['ainame']=$agent['ainame'];
		$goodsagent['user_name']=$agent['user_name'];
		$goodsagent['tplid']=$agent['id'];
		$goodsagent['status']=2;
		$this->model('agent')->adduseragents($goodsagent);
	}
	exi('切换成功！','',$_RQ['isres']?'':'/index/');
}